import java.io.File;

/**
 * java -Djava.security.manager -Djava.security.policy==PermissionTest.policy PermissionTest
 */
public class PermissionTest {
  public static void main(String[] args) {

    SecurityManager manager = System.getSecurityManager();

    File f = new File("");
    System.out.println(System.getProperty("user.dir"));
    System.out.println(f.getAbsoluteFile().getParent());

    try {
      manager.checkRead("../test");
      System.out.println("读权限可用!");
    } catch (SecurityException e) {
      System.out.println("读权限不可用!");
      System.out.println(e.getMessage());
    }

    try {
      manager.checkWrite("../test");
      System.out.println("写权限可用!");
    } catch (SecurityException e) {
      System.out.println("写权限不可用!");
      System.out.println(e.getMessage());
    }

    try {
      manager.checkDelete("../test");
      System.out.println("删除权限可用!");
    } catch (SecurityException e) {
      System.out.println("删除权限不可用!");
      System.out.println(e.getMessage());
    }

    try {
      manager.checkExec(f.getAbsoluteFile().getParent() + File.separator + "test");
      System.out.println("执行权限可用!");
    } catch (SecurityException e) {
      System.out.println("执行权限不可用!");
      System.out.println(e.getMessage());
    }

    System.out.println("=======检查应用程序本身目录文件权限=======");
    try {
      manager.checkRead("TEM");
      System.out.println("TEM 读权限可用!");

    } catch (SecurityException e) {
      System.out.println("TEM 读权限不可用!");
      System.out.println(e.getMessage());
    }

    try {
      manager.checkWrite("TEM");
      System.out.println("TEM 写权限可用!");

    } catch (SecurityException e) {
      System.out.println("TEM 写权限不可用!");
      System.out.println(e.getMessage());
    }

    try {
      manager.checkDelete("TEM");
      System.out.println("TEM 删除权限可用!");

    } catch (SecurityException e) {
      System.out.println("TEM 删除权限不可用!");
      System.out.println(e.getMessage());
    }

    try {
      manager.checkExec(System.getProperty("user.dir") + File.separator + "TEM");
      System.out.println("TEM 执行权限可用!");
    } catch (SecurityException e) {
      System.out.println("TEM 执行权限不可用!");
      System.out.println(e.getMessage());
    }

    System.out.println("Fine.");

  }
}